﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace SilverlightDemoApp.CommandService
{
    public static class ButtonService
    {
        #region CommandParameter

        /// <summary>
        /// CommandParameter Attached Dependency Property
        /// </summary>
        public static readonly DependencyProperty CommandParameterProperty =
            DependencyProperty.RegisterAttached("CommandParameter", typeof (string), typeof (ButtonService),
                                                new PropertyMetadata(OnCommandParameterChanged));

        /// <summary>
        /// Gets the CommandParameter property.
        /// </summary>
        public static string GetCommandParameter(DependencyObject d)
        {
            return (string) d.GetValue(CommandParameterProperty);
        }

        /// <summary>
        /// Sets the CommandParameter property.
        /// </summary>
        public static void SetCommandParameter(DependencyObject d, string value)
        {
            d.SetValue(CommandParameterProperty, value);
        }

        /// <summary>
        /// Handles changes to the CommandParameter property.
        /// </summary>
        private static void OnCommandParameterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
        }

        #endregion

        #region Command

        /// <summary>
        /// Command Attached Dependency Property
        /// </summary>
        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.RegisterAttached("Command", typeof (ICommand), typeof (ButtonService),
                                                new PropertyMetadata(OnCommandChanged));

        /// <summary>
        /// Gets the Command property.
        /// </summary>
        public static ICommand GetCommand(DependencyObject d)
        {
            return (ICommand) d.GetValue(CommandProperty);
        }

        /// <summary>
        /// Sets the Command property.
        /// </summary>
        public static void SetCommand(DependencyObject d, ICommand value)
        {
            d.SetValue(CommandProperty, value);
        }

        /// <summary>
        /// Handles changes to the Command property.
        /// </summary>
        private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is Button)
            {
                var parameter = d.GetValue(CommandParameterProperty) as string;
                var b = d as Button;
                var c = e.NewValue as ICommand;
                b.Click += delegate { c.Execute(parameter); };
            }
        }

        #endregion
    }
}